//
//  BKMain_ActiveCell.m
//  bestkeep
//
//  Created by utouu666 on 17/2/14.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKMain_ActiveCell.h"
#import "BKMain_PicViewCell.h"
#import "BKMainCommon.h"

@interface BKMain_ActiveCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UILabel *bkHot;
@property (nonatomic, strong) UICollectionView *laterCollectionView;
@property(nonatomic,strong)NSString *type;

@end



@implementation BKMain_ActiveCell


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

-(void)initialize{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _laterCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _laterCollectionView.backgroundColor = [UIColor whiteColor];
    
    [_laterCollectionView registerClass:[BKMain_PicViewCell class] forCellWithReuseIdentifier:@"BKMain_PicViewCell"];
    _laterCollectionView.delegate = self;
    _laterCollectionView.dataSource = self;
    _laterCollectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_laterCollectionView];
    [_laterCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.top.mas_equalTo(self.mas_top);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    
    return  1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    BKMain_PicViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BKMain_PicViewCell" forIndexPath:indexPath];
    
    NSDictionary *itemDic = [self.dataArray objectAtIndex:indexPath.row];
    MiddleBannerModel *model = [[MiddleBannerModel alloc]init];
    [model setValuesForKeysWithDictionary:itemDic];
    switch (self.dataArray.count) {
        case 1:
            [cell.picImageView load:model.img placeholderImage:[UIImage imageNamed:@"BKMain_banner-1"]];
            break;
        case 2:
            [cell.picImageView load:model.img placeholderImage:[UIImage imageNamed:@"BKMain_banner-2"]];
            break;
        default:
            [cell.picImageView load:model.img placeholderImage:[UIImage imageNamed:@"BKMain_banner-3"]];
            break;
    }
    
    
    
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(0.1, 150);
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (self.dataArray.count) {
        case 1:
            return  CGSizeMake(ScreenWidth, ScreenWidth/375* 65);
            break;
        case 2:
            return  CGSizeMake(ScreenWidth/2, ScreenWidth/375* 65);
            break;
        default:
            return CGSizeMake(ScreenWidth / 3,ScreenWidth/375* 65);
            break;
    }
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 4;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    BKMainViewController *mainPage  = (BKMainViewController *)self.nextResponder.nextResponder.nextResponder;
        NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.item];
    
    
    BannerModel *model = [[BannerModel alloc] init];
    [model setValuesForKeysWithDictionary:dic];
    
    BKMain_WebViewController *h5 = [[BKMain_WebViewController alloc]init];
    h5.webViewUrl = model.code;
    h5.webViewTitle = model.name;
    h5.type = model.type;
    h5.shareFlag = model.shareFlag;
    h5.shareCode = model.shareCode;
    h5.shareImg = model.shareImg;
    h5.shareTitle = model.shareTitle;
    h5.shareContent = model.shareContent;
    h5.type_Share = self.type;
    if ([[NSString stringWithFormat:@"%@",model.type] isEqualToString:@"4"]) {
        [mainPage.navigationController pushViewController:h5 animated:YES];
    }else if ([[NSString stringWithFormat:@"%@",model.type] isEqualToString:@"6"]){
        if ([BKMainCommon islogin]) {
            [mainPage.navigationController pushViewController:h5 animated:YES];
        }else{
            Mediator *mediator = [Mediator sharedInstance];
            [mediator userGoToLoginFromOrignalController:mainPage finishBlock:^ {
                
            }];
            
        }
    }else{
        [mainPage pushWithType:model.type code:model.code name:model.name saleTypes:@""];
    }
}

+(CGSize)calculateSize:(id)data{
    if(!data || ![data isKindOfClass:[NSArray class]]) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(SCREENWIDTH, 65*SCREENWIDTH/375);
}

- (void)fillData:(id)data{
    if (!data) {
        return;
    }
    self.dataArray = data[@"data"];
    self.type = data[@"type"];
    [self.laterCollectionView reloadData];
    
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
@end
